﻿#coding: utf-8

#Import Modules
import pygame
import Primitives
import Properties
from Graphics import *

from Units import Curva,Snake
from Util import *

class CurveEntitie(Primitives.GameObject):

	def __init__(self,id):
		super(CurveEntitie,self).__init__(id)
		
		self.image, self.rect = load_surface_from_image(Properties.imgCurva, -1)
		self.imageOriginal = self.image.copy()	
		self.imageOriginalCurva = None
		self.unit = Curva(self.image.get_size())

		self.drawCurveState = False
		self.mousePos1 = 0
		self.mousePos2 = 0
		
		
	def update(self):			
		#return
		if self.drawCurveState:
			mousePos1 = self.getRelativeImageMousePosition(self.mousePos1[0],self.mousePos1[1])
			mousePos2 = self.getRelativeImageMousePosition(self.mousePos2[0],self.mousePos2[1])

			if mousePos1[0] is not None and mousePos2[1] is not None:
				self.unit.addRect( mousePos1,mousePos2 )
				r = desenhar_reta_curva( mousePos1,mousePos2,self.image,RED )			
			
			self.mousePos1,self.mousePos2 = self.mousePos2,pygame.mouse.get_pos()

	def goDraw(self):
		self.image = self.imageOriginal.copy()
		self.unit = Curva(self.image.get_size())
		self.drawCurveState = True

		self.mousePos1 = pygame.mouse.get_pos()
		self.mousePos2 = pygame.mouse.get_pos()

		
		
		#Desenha automatico
		'''
		pontos_teste = [  (191,134),  (187,137),  (183,139),  (180,142),  (178,145),  (176,148),  (175,151),  (174,155),  (175,160),  (177,163),  (179,168),  (181,172),  (185,175),  (189,178),  (192,179),  (196,180),  (200,181),  (204,183),  (208,184),  (213,185),  (217,185),  (222,186),  (227,186),  (231,186),  (236,185),  (240,184),  (244,183),  (247,182),  (251,180),  (253,178),  (255,175),  (257,171),  (258,167),  (258,165),  (257,161),  (255,156),  (253,151),  (252,147),  (250,143),  (246,139),  (243,136),  (240,135),  (237,133),  (233,131),  (228,129),  (224,129),  (220,129),  (216,129),  (212,129),  (208,130),  (204,131),(199,131),(200,131)     ]
		
		pi = pontos_teste[0]
		p1 = pi
		for i in range( 1,len(pontos_teste) ):
			p2 = pontos_teste[i]
			
			self.unit.addRect( p1,p2,True )
			r = desenhar_reta_curva( p1,p2,self.image,RED )	
			
			p1 = p2
			
		r = desenhar_reta_curva( p2,pi,self.image,RED )	
		'''
		#Desenha automatico fim
		
		self.imageOriginalCurva = self.image.copy()
		
		
	def stopDraw(self):
		self.drawCurveState = False
		
		self.mousePos1 = 0
		self.mousePos2 = 0		
		

		self.reset_image()
		self.unit.desenhar_curva(self.image)
		
		self.imageOriginalCurva = self.image.copy()
		
	def reset_image(self):
		self.image = self.imageOriginal.copy()
		
	def getRelativeImageMousePosition(self,mousex,mousey):
		
		if( ( mousex<self.rect.x or mousex>(self.rect.x+self.rect.width) ) or
			( mousey<self.rect.y or mousey>self.rect.y+self.rect.height) ):
			
			return (None,None)
		else:
			return ( (mousex - self.rect.x ),(mousey-self.rect.y) )
			
class SnakeEntitie(Primitives.GameObject):
	
	def __init__(self,id):
		super(SnakeEntitie,self).__init__(id)
		
		self.image, self.rect = None,None
		self.update_flag = False
		
	def set_curva(self,curva):
		self.curva_entitie = curva
		self.unit = Snake(load_image(Properties.imgCurva),curva.unit)		
		self.str_indice = ""
		
		self.image = curva.image
		self.rect = curva.rect
		
	def setar_quina(self):
		self.unit.xk.pontos[ ( int(self.str_indice) ) ].quina = not ( self.unit.xk.pontos[ ( int(self.str_indice) ) ].quina )
		self.str_indice = ""
		
	def setar_alfa_especial(self):
		indice = int( self.str_indice.split(",")[0] )
		coef = self.str_indice.split(",")[1]
		
		Ponto.alfas_especiais[indice] = float( coef )
		self.str_indice = ""
		
	def setar_beta_especial(self):
		indice = int( self.str_indice.split(",")[0] )
		coef = self.str_indice.split(",")[1]
		
		Ponto.betas_especiais[indice] = float( coef )
		self.str_indice = ""		
		
	def setar_gama_especial(self):
		indice = int( self.str_indice.split(",")[0] )
		coef = self.str_indice.split(",")[1]
		
		Ponto.gamas_especiais[indice] = ( float( coef ), float(coef) )
		self.str_indice = ""	

	def setar_forca_central(self):
		indice = int( self.str_indice.split(",")[0] )
		coef = self.str_indice.split(",")[1]
		
		Ponto.forca_central[indice] = not( Ponto.forca_central[indice] )
		self.str_indice = ""			
		
		
	def update(self):

		if self.update_flag:
			img = self.curva_entitie.imageOriginal.copy()
			
			pi = self.unit.pontos[0]
			p1 = pi

			for i in range(1,len(self.unit.pontos) ):
				p2 = self.unit.pontos[i]
				
				desenhar_reta_snake( p1,p2,img,GREEN )
				
				p1 = p2
				
			desenhar_reta_snake( p2,pi,img,GREEN )
			desenhar_reta_snake( self.unit.ponto_medio,self.unit.ponto_medio,img,RED )
			
			self.curva_entitie.image = img
			
			#salvar_imagem( self.unit.img,Properties.imgTemp )
			
			'''
			img_d = ImageDebug.get_instance(img)
			img_d.print_image()
							
			salvar_imagem( img_d.surf, Properties.imgDebug )			
			'''
			
		self.update_flag = False
		
	def minimizar(self):
		self.unit.minimizar()
		self.update_flag = True
		
	def imprimir(self):
		self.unit.debug.imprimir()
		
			